package com.jukte.recipe.db;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.jukte.recipe.models.Ingredient;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class DataBaseHelper extends SQLiteAssetHelper{
	private static final String DATABASE_NAME = "recipe";
	private static final int DATABASE_VERSION = 1;
	
	public DataBaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
		// you can use an alternate constructor to specify a database location 
		// (such as a folder on the sd card)
		// you must ensure that this folder is available and you have permission
		// to write to it
		//super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
		
	}
	
	
	public long insertIntoTable(String tableName,String nullCulumnHak,ContentValues inVal)
	{
		SQLiteDatabase db = getWritableDatabase();
		return db.insert(tableName, nullCulumnHak, inVal);
	}
	
	
	
	public void executeUpdateQuery(String sql)
	{
		SQLiteDatabase db = getWritableDatabase();
		db.execSQL(sql);
		
	}
	
	public void executeDeleteQuery(String table, String whereClause, String[] whereArgs)
	{
		SQLiteDatabase db = getWritableDatabase();
		db.delete(table, whereClause, whereArgs);
		
	}
	
	public Cursor executeRawQuery(String sqlQuery,String[] selectionArgs)
	{
		SQLiteDatabase db = getWritableDatabase();
		Cursor c=db.rawQuery(sqlQuery, selectionArgs);
		return c;
		
	}
	
	public Cursor getAllByForeignId(String tableName,String foreignFieldName,int id)
	{
		SQLiteDatabase db = getReadableDatabase();
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		String sqlTables = tableName;

		qb.setTables(sqlTables);
		Cursor c = qb.query(db, null, foreignFieldName+"="+id, null,
				null, null, null);

		//c.moveToFirst();
		return c;
	}
	
	public Cursor getById(String tableName,int id)
	{
		SQLiteDatabase db = getReadableDatabase();
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		String sqlTables = tableName;

		qb.setTables(sqlTables);
		Cursor c = qb.query(db, null,"id="+id, null,
				null, null, null);
		return c;
	}
	
	public Cursor getAll(String tableName) {

		SQLiteDatabase db = getReadableDatabase();
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		String sqlTables = tableName;

		qb.setTables(sqlTables);
		Cursor c = qb.query(db, null, null, null,
				null, null, null);

		//c.moveToFirst();
		return c;
	}


	public void updateIngredientsCart(List<Ingredient> ingredients) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = getReadableDatabase();
		 db.beginTransaction();
		    try {
		        for(int i=0;i<ingredients.size();i++) {
		        	int id=ingredients.get(i).getId();
		        	int isAddedCart=ingredients.get(i).getIsAddedToCart();
		        	String sql="UPDATE ingredients SET is_added_cart="+isAddedCart+" WHERE  id = "+id;
		            db.execSQL(sql);
		            }
		        db.setTransactionSuccessful();
		    } finally {
		        db.endTransaction();
		    }
	}
}
